/**
 * 树控件通用工具类.
 */
define(['jquery'], function ($) {
	var utils = {
			/**
	    	 * 构建jstree 能识别的json。
	    	 */
	    	getJsTreeJson: function(data, treeOpt){
	    		if (!data || !data.length) return [];
	    		var node,pNode,jsTreeData = [],
	    			me = this,
	    			treeCodeField = treeOpt.treeCodeField,
	    			treeParentField = treeOpt.treeParentField;
	    		//按itemcode排序.
	    		data.sort(function(a, b){
		        	return a[treeCodeField].localeCompare(b[treeCodeField]);
		        }); 
	    		if(treeParentField){
	    			var datas = this.doParentData(data,treeOpt);
	    			for(var i=0,j=datas.length; i<j; i++){
		    			node = me.createParentNode(datas[i],treeParentField, treeOpt);
	    				jsTreeData.push(node);
		    		}
	    		}else{
		    		for(var i=0,j=data.length; i<j; i++){
		    			node = me.createNode(data[i], treeOpt);
		    			pNode = me.getpNode(node,jsTreeData);
		    			if(pNode){
		    				pNode.children.push(node);
		    			}else{
		    				jsTreeData.push(node);
		    			}
		    		}
	    		}
	    		 return jsTreeData;
	    	},
	    	/**
	    	 * 处理父节点.
	    	 */
	    	doParentData: function(data, treeOpt){
	    		var len = data.length,
    				treeCodeField = treeOpt.treeCodeField,
    				treeParentField = treeOpt.treeParentField;
	    		for(var i=0; i<len; i++){
	    			var pv = data[i][treeParentField];
	    			var bool = false;
	    			for(var j=0; j<len; j++){
	    				if(data[j][treeCodeField] == pv){
	    					bool = true; 
	    					break;
	    				}
	    			}
	    			if(!bool){
	    				data[i].parent = "#";
	    			}
	    		}
	    		return data;
	    	},
			/**
	    	 * 创建parent字段节点.
	    	 * 构建id和parent结构的数据
	    	 * @param {Object} data 数据对象
	    	 * @param {String} treeParentField 父节点字段
	    	 */
	    	createParentNode: function(data,treeParentField, treeOpt){
	    		var node = data,
	    			treeCodeField = treeOpt.treeCodeField,
	    			treeTextField = treeOpt.treeTextField,
	    			treeCodePolicy = treeOpt.treeCodePolicy;
	    		node.id = data[treeCodeField];
	    		node.text = data[treeTextField];
	    		if(node.parent != "#"){
	    			node.parent = data[treeParentField];
	    		}
	    		var nodelen = treeCodePolicy.split("-");
	    		var treelen = 0;
	    		for(var i=0,j=nodelen.length; i<j; i++){
	    			if(node[treeCodeField].length == treelen + parseInt(nodelen[i])){
	    				break;
	    			} else{
	    				treelen = treelen + parseInt(nodelen[i]);
	    			}
	    		}
	    		node.pid = data[treeCodeField].substr(0,treelen);
	    		return node; 
	    	},
	    	
	    	/**
	    	 * 创建节点.
	    	 * 构建id和children结构的数据
	    	 * @param {Object} data 树节点数据对象
	    	 */
	    	createNode: function(data,treeOpt){
	    	   var node = data,
	    	   	   treeCodeField = treeOpt.treeCodeField,
	    	   	   treeTextField = treeOpt.treeTextField,
	    	   	   treeValueField = treeOpt.treeValueField,
	    	   	   treeCodePolicy = treeOpt.treeCodePolicy,
	    	   	   nodelen = treeCodePolicy.split("-"),
	    	   	   treelen = 0;
	    	   node.id = data[treeCodeField];
	    	   node.text = data[treeTextField];
	    	   for(var i=0,j=nodelen.length; i<j; i++){
	    		   if(node[treeCodeField].length == treelen + parseInt(nodelen[i])){
	    			   break;
	    		   } else{
	    			   treelen = treelen + parseInt(nodelen[i]);
	    		   }
	    	   }
    	   	   node.pid = data[treeCodeField].substr(0,treelen);
	    	   node.children = [];
	    	   return node;  
	    	},
	    	/**
	    	 * 获取上级节点.
	    	 */
	    	getpNode: function(node,jsTreeData){
	    	    for(var i=0,j=jsTreeData.length; i<j; i++){
	    	    	var pNode = jsTreeData[i];
	    	    	if(!node.pid){
	    	    		return null;
	    	    	}else if(node.pid == pNode.id){
	    	    		return pNode;
	    	    	}else if(pNode.children.length){
	    	    		pNode = this.getpNode(node, pNode.children);
	    	    	}
	    	    } 
	    	    return pNode;
	    	},
	    	/**
	    	 * 树控件定位，滚动条跟着跳转.
	    	 */
	    	fixedData: function(){
	    		
	    	}
	}
	return utils;
});